package com.example.project_out_in;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;



public class Fragment_in  extends Fragment{

	  private static final String TAG = "CamTestActivity";
		Preview preview;
		Button buttonClick;
		Camera camera;
		String fileName;
		Activity act;
		Context ctx;

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		final View root = inflater.inflate(R.layout.in_activity, container, false);
		int chieu = getResources().getConfiguration().orientation;
		
        ctx = root.getContext();
		act = this.getActivity();
		
		//LinearLayout ll = new LinearLayout(ctx);
		
		FrameLayout flayout = (FrameLayout)root.findViewById(R.id.preview);
		//LinearLayout layout = (LinearLayout)flayout.findViewById(R.id.linearLayout_camera);
		
		Log.d(TAG, "Gets the layout params that will allow you to resize the layout");
		// Gets the layout params that will allow you to resize the layout
		//LinearLayout.LayoutParams params =  layout.getLayoutParams();
		//LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) layout.getLayoutParams();
		LayoutParams params = new LayoutParams(0, LayoutParams.MATCH_PARENT);
		//params.weight = 3.0f;
		// Changes the height and width to the specified *pixels*
		Log.d(TAG, "Changes the height and width to the specified *pixels*");
		if(chieu==1)
		{
			
			params.height = 600;
			params.width = 390;
		}
		else
		{
			
			params.height = 390;
			params.width = 600;
		}
		Log.d(TAG, "Set param");
		flayout.setLayoutParams(params);
		Log.d(TAG, "add pram layout");
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		//getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		preview = new Preview(ctx, (SurfaceView)root.findViewById(R.id.surfaceView));
		//Log.d(TAG, "b1");
		//preview.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		((FrameLayout) root.findViewById(R.id.preview)).addView(preview);
		preview.setKeepScreenOn(true);
		//Log.d(TAG, "b2");
		
		buttonClick = (Button) root.findViewById(R.id.buttonClick);
		Log.d(TAG, "set buttonClick");
		buttonClick.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//				preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
				camera.takePicture(shutterCallback, rawCallback, jpegCallback);
				 //Log.d(TAG, "setOnClickListener takePicture");
			}
		});
		Log.d(TAG, "set buttonClick setOnLongClickListener");
		buttonClick.setOnLongClickListener(new OnLongClickListener(){
			@Override
			public boolean onLongClick(View arg0) {
				camera.autoFocus(new AutoFocusCallback(){
					@Override
					public void onAutoFocus(boolean arg0, Camera arg1) {
						//camera.takePicture(shutterCallback, rawCallback, jpegCallback);
					}
				});
				return true;
			}
		});
		
		//Log.d(TAG, "retun root");
        
        return root;

    }
	public void setWidthHeight(View v, int width, int height){
	    LayoutParams lp;
	    lp = (LayoutParams) v.getLayoutParams();
	    lp.width = width;
	    lp.height = height;
	    v.setLayoutParams(lp);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		//      preview.camera = Camera.open();
		Log.d(TAG, "Came ra open");
		camera = Camera.open();
		Log.d(TAG, "Came ra startPreview");
		camera.startPreview();
		Log.d(TAG, "preview.setCamera");
		preview.setCamera(camera);
	}

	@Override
	public void onPause() {
		if(camera != null) {
			camera.stopPreview();
			preview.setCamera(null);
			camera.release();
			camera = null;
		}
		super.onPause();
	}

	private void resetCam() {
		camera.startPreview();
		preview.setCamera(camera);
	}

	ShutterCallback shutterCallback = new ShutterCallback() {
		public void onShutter() {
			 //Log.d(TAG, "onShutter'd");
		}
	};

	PictureCallback rawCallback = new PictureCallback() {
		public void onPictureTaken(byte[] data, Camera camera) {
			 //Log.d(TAG, "onPictureTaken - raw");
		}
	};

	PictureCallback jpegCallback = new PictureCallback() {
		public void onPictureTaken(byte[] data, Camera camera) {
			FileOutputStream outStream = null;
			try {
				File direct = new File(Environment.getExternalStorageDirectory() + "/Building_Msmart");

				   if(!direct.exists())
				    {
				        direct.mkdir(); // tao thu muc
				          
				    }
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.append(Environment.getExternalStorageDirectory()+ "/Building_Msmart/photo_");
				stringBuilder.append(System.currentTimeMillis());
				stringBuilder.append(".jpg");
				// Write to SD Card
				fileName = stringBuilder.toString();
				
				//Toast.makeText(ctx, "chuoi: " + Environment.getExternalStorageDirectory() + "/Building_Msmart", 1).show();
				//Toast.makeText(ctx, "chuoi: " + fileName, 1).show();
				File file = new File(fileName);
				//fileName = String.format("/sdcard/camtest/%d.jpg", System.currentTimeMillis());
				outStream = new FileOutputStream(file);
				outStream.write(data);
				outStream.close();
				//Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);

				resetCam();

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
			}
			//Log.d(TAG, "onPictureTaken - jpeg");
		}
	};
	

}
